package comWorld.account.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import webManage.UrlTools;
import comWorld.user.UserStatus;

/**
 * Servlet Filter implementation class UserLoginPageFilter
 */
@WebFilter("/account/login.jsp")
public class UserLoginPageFilter implements Filter {

    /**
     * Default constructor. 
     */
    public UserLoginPageFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpSession session = ((HttpServletRequest)request).getSession();
		UserStatus visitorStatus = (UserStatus) session.getAttribute("visitorStatus");
		if (visitorStatus != null && visitorStatus.isLogin())
			((HttpServletResponse)response).sendRedirect(UrlTools.WEBROOTURL);
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
